package web

import (
	"gitee.com/fanshuaiself/sidecar/util"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

type WebPlugin struct {
	engine *gin.Engine
	closeFn func()
}

func (w *WebPlugin)	Initialize(config *util.Config, closeFunc func()) {
	w.closeFn = closeFunc
	w.engine = gin.Default()
	w.engine.GET("/kill", func(ctx *gin.Context) {
		if w.closeFn != nil {
			w.closeFn()
		}
		ctx.String(200, "ok")
	})
}

func (w *WebPlugin)	Start() error {
	go w.doStart()
	return nil
}

func (w *WebPlugin)	Stop () {
	log.Print("web server stoped")
}

func (w *WebPlugin) doStart() {
	log.Print("web开始启动")
	err := w.engine.Run("8081")
	if err != nil {
		log.Fatal("start web server failed")
	}
	log.Print("web启动完成")
}